import { observable, action, computed, runInAction } from 'mobx'
import { post } from 'lib/net'
import CommonVM from 'cvm/common'
import * as api from 'lib/constants/api'
import Toast from 'components/toast'

export default class TalentVM extends CommonVM {
  constructor (props) {
    super()
    this.props = props
    this.fetchData()
  }

  // 当前选中tabs
  @observable weekSelected = true
  // 内容
  @observable data = []

  // 周样式
  @computed get weekBtnStyle () {
    return this.weekSelected
    ? 'flex-1 current-select'
    : 'flex-1'
  }
  // 月样式
  @computed get monthBtnStyle () {
    return this.weekSelected
    ? 'flex-1'
    : 'flex-1 current-select'
  }
  // 标题
  @computed get title () {
    return this.weekSelected
    ? '上周排名前十的种植达人'
    : '上月排名前十的种植达人'
  }

  // 选中标签
  @action handleSelect = selected => {
    this.weekSelected = selected
    this.fetchData()
  }

  // 获取数据
  @action fetchData = async () => {
    let url = this.weekSelected
    ? api.BEAN_WEEK
    : api.BEAN_MONTH
    Toast.loading()
    let res = await post(url)
    if (res.state === 1) {
      if (!res.data) {
        res.data = []
      }
      runInAction(() => {
        this.data.splice(0, this.data.length, ...res.data)
      })
      Toast.loading(2)
    } else {
      Toast.fail('获取')
    }
  }
}
